/*
 * Copyright (c)  2015~2020, xforceplus
 * All rights reserved.
 * Project:datacare-orm
 * Id: SelectMapperInvoke.java   2020-10-08 00-12-30
 * Author: Evan
 */
package cn.datacare.orm.mapper.inovke;

import cn.datacare.orm.manager.impl.SQLManager;
import cn.datacare.orm.mapper.MapperInvoke;
import cn.datacare.orm.mapper.MethodDesc;
import cn.datacare.orm.mapper.parameter.SelectQueryParameter;

import java.lang.reflect.Method;
import java.util.Map;

/**
 * <p>
 * Title:
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: 2015~2020
 * </p>
 * <p>
 * Company/Department: xforceplus
 * </p>
 *
 * @author Evan
 * <b>Creation Time:</b> 2020-10-08 00-12-30
 * @since V1.0
 */
public class SelectMapperInvoke implements MapperInvoke {

    @Override
    public Object call(SQLManager sm, Class entityClass, String sqlId, Method m, Object[] args) {

        MethodDesc desc = MethodDesc.getMethodDescBySqlId(sm,entityClass,m,sqlId);
        SelectQueryParameter parameter = (SelectQueryParameter)desc.parameter;
        Map map = (Map)parameter.get(args);
        Class returnType = desc.resultType;
        if(parameter.hasRangeSelect()){
            long offset ,size ;
            offset = ((Number)args[parameter.getStartPos()]).longValue();
            size = ((Number)args[parameter.getSizePos()]).longValue();
            return sm.select(sqlId, returnType, map,offset,size);
        }else{
            return sm.select(sqlId, returnType, map);
        }

    }
}
